﻿using Mt.Site.Application.DataExporting.Excel.NPOI;
using Mt.Site.Application.Dto;
using Mt.Site.Application.Members.BackEnd.Dto;
using Mt.Site.Core.Storage;
using System.Collections.Generic;

namespace Mt.Site.Application.Members.BackEnd
{
    public class ExportMemberHelper : NpoiExcelExporterBase, IExportMemberHelper
    {
        public ExportMemberHelper(ITempFileCacheManager tempFileCacheManager) 
            : base(tempFileCacheManager)
        {
        }

        public FileDto MemberExportDataList(List<MemberExportInput> input)
        {
            return CreateExcelPackage(
                "MemberInfo.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.CreateSheet("MemberInfo");

                    AddHeader(
                        sheet,
                        L("Email"),
                        L("LoginName"),
                        L("FirstName"),
                        L("LastName"),
                        L("Company"),
                        L("PhoneNumber"),
                        L("JobTitle"),
                        L("Fax"),
                        L("WebPage"),
                        L("Address"),
                        L("Address2"),
                        L("City"),
                        L("Province"),
                        L("ZipCode"),
                        L("Country"),
                        L("Received"),
                        L("RegisterFrom"),
                        L("registerDate")
                        );

                    AddObjects(
                                sheet, 2, input,
                                _ => _.Email,
                                _ => _.LoginName,
                                _ => _.FirstName,
                                _ => _.LastName,
                                _ => _.Company,
                                _ => _.Phone,
                                _ => _.JobTitle,
                                _ => _.Fax,
                                _ => _.WebPage,
                                _ => _.Address,
                                _ => _.Address1,
                                _ => _.City,
                                _ => _.Province,
                                _ => _.ZipCode,
                                _ => _.Country,
                                _ => _.IsReceive,
                                _ => _.RegisterForm,
                                _ => _.registerDate
                                );

                    for (var i = 0; i < 11; i++)
                    {
                        sheet.AutoSizeColumn(i);
                        var len = 50 * 256;
                        if (sheet.GetColumnWidth(i) > len)
                        {
                            sheet.SetColumnWidth(i, len);
                        }
                    }
                });
        }
    }
}
